function BeforeSave(payload) {
    // console.log("---BeforeSave - START1---")
    // console.log(payload)
    const user_id = Process("session.Get", "user_id");
    console.log("user_id===before=>", user_id)
    payload.user_id = user_id;
    // let allp = Process("session.Dump");
    // console.log("allp=>", allp);

    // console.log("---BeforeSave END1---")
    return payload;
}
function batchSave(payload) {
    console.log("---START-device-batchSave---", payload)
    // 检查当前用户是否有该设备
    let user_id = Process("session.Get", "user_id");
    console.log("user_id===>", user_id)
    // 检查当前是否有重复导入的数据，有的话，则更新，没有的话，则新增
    payload.data.forEach(item => {
        console.log("item===>", item.device_id)

        let _device = Process("models.device.Get", { wheres: [{ "column": "id", "op": "=", "value": item.device_id }, { "column": "user_id", "op": "=", "value": user_id }] });
        if (_device) {
            try {
                let res = Process("models.device_used.Save", item);
                console.log("import---->" + item.device_id + " " + item.used_date + "[" + res + "] 数据成功");
            } catch (error) {
                console.log("该设备导入失败，数据已存在" + item.device_id + " " + item.used_date + error);
            }

        } else {
            console.log("该设备导入失败，请检查设备是否存在" + item.device_id + " " + item.used_date);
        }
    });
    return payload;
}

function Save(payload) {
    console.log("---START-device-save---", payload)

    let user_id = Process("session.Get", "user_id");

    payload.user_id = user_id;
    payload.oper_id = user_id;
    console.log("payload-user_id:", payload.user_id);

    let serial_num = payload.serial_num;
    console.log("serial_num--->", serial_num)
    // if there is same serial no device exist, if exist, it will update, or else it will add new device in system
    var devices = Process("models.device.get", {
        wheres: [
            { column: "serial_num", op: "eq", value: serial_num },
            //  { column: "status", value: "启用" },
        ],
        limit: 1,
    });
    console.log("devices----->", devices);
    if (!devices || !devices.length) {
        var res = Process("models.device.Save", payload);
        console.log("res--save-->", res);
        return { "id": res };
    } else {
        console.log(devices[0].name + "[" + serial_num + "]" + devices[0].id + "设备已存在");
        console.log("payload userid " + payload.user_id);
        console.log("database userid " + devices[0].user_id);
        if (payload.user_id != devices[0].user_id && payload.user_id != 1) {
            throw new Exception("只能admin或本人能修改", 400);
        }
        if (payload.user_id == 1) {
            payload.oper_id = payload.user_id;
            delete payload.user_id; // 如果是admin账号则不能更新使用者字段user_id 
        }
        Process("models.device.Update", devices[0].id, payload);
        console.log("res--update-->");

        return { "id": devices[0].id };
    }
}

// 已暂时废弃
function Save1(payload) {
    console.log("---START-device-save1---")
    let allp = Process("session.Dump");
    console.log("allp=>", allp);

    let user_id = Process("session.Get", "user_id");

    console.log("user_id:", user_id);


}
